package com.tojabo.util;

import java.util.ArrayList;
import java.util.List;

import com.tojabo.entity.DoReMiFa;

public class SoundUtil {
	
	private static double onePaitime = 700;//一拍 时间
	private static int standard_do=51;//标准音do 是在数组的位置  c1:3,c2:15,c3:27,c4:39,c5:51,c6:63,c7:75,c8:87
	
	private static List<String> soundFileNames=new ArrayList<String>();
	private static String[] soundNames=null;
	
	public static void main(String[] args) {
		
		int glo = 20;
		
		int pan = glo/7;
		int but = glo%7;
		
		System.out.println("but:"+but+" pan:"+pan);
	}
	
	public static DoReMiFa parserMusicBook(String Stringline){		
		DoReMiFa doremifa = new DoReMiFa();
		
		String[] pitch_timez = Stringline.split(",");
		
		String pitch = pitch_timez[0];
		String timez = pitch_timez[1];

		//分析音名
		doremifa=parserPitchName(doremifa,pitch);
		
		
		//持续时间
		String[] son_mom=timez.split("/");
		double son = Double.parseDouble(son_mom[0]);
		double mom = Double.parseDouble(son_mom[1]);
		
		double soundTime=son/mom*onePaitime;
		doremifa.setDurationTime((int)soundTime);
		return doremifa;
	}
	
	public static DoReMiFa parserPitchName(DoReMiFa doremifa,String soundSyn1){
		int soundindex = 0;
		if(soundSyn1.contains("*")){
			int count=StringUtil.CharCount(soundSyn1, '*');
			soundSyn1=soundSyn1.replace("*", "");
			soundindex=getSoundIndex(soundSyn1);
			soundindex=soundindex+12*count;
		}else if(soundSyn1.contains("%")){
			int count=StringUtil.CharCount(soundSyn1, '%');
			soundSyn1=soundSyn1.replace("%", "");			
			soundindex=getSoundIndex(soundSyn1);
			soundindex=soundindex-12*count;
		}else{	
			soundindex=getSoundIndex(soundSyn1);			
		}
		
		if(soundFileNames.size()==0){
			readSoundFileFromTxt();
		}
		String soundfilepath = soundFileNames.get(soundindex);		
		doremifa.setPitchFile(soundfilepath);
		
		if(soundNames==null){
			splitSoundNames();
		}
		String soundname=soundNames[soundindex];
		doremifa.setPitchName(soundname);
		
		return doremifa;
	}
	
	public static int getSoundIndex(String pitchSymbol){
		int index = 0;
		if(pitchSymbol.equals("1b")){
			index=standard_do-1;
		}else if(pitchSymbol.equals("1")){
			index=standard_do;
		}else if(pitchSymbol.equals("1#")){
			index=standard_do+1;
		}else if(pitchSymbol.equals("2b")){
			index=standard_do+1;
		}else if(pitchSymbol.equals("2")){
			index=standard_do+2;
		}else if(pitchSymbol.equals("2#")){
			index=standard_do+3;
		}else if(pitchSymbol.equals("3b")){
			index=standard_do+3;
		}else if(pitchSymbol.equals("3")){
			index=standard_do+4;
		}else if(pitchSymbol.equals("3#")){
			index=standard_do+5;
		}else if(pitchSymbol.equals("4")){
			index=standard_do+5;
		}else if(pitchSymbol.equals("4#")){
			index=standard_do+6;
		}else if(pitchSymbol.equals("5b")){
			index=standard_do+6;
		}else if(pitchSymbol.equals("5")){
			index=standard_do+7;
		}else if(pitchSymbol.equals("5#")){
			index=standard_do+8;
		}else if(pitchSymbol.equals("6b")){
			index=standard_do+8;
		}else if(pitchSymbol.equals("6")){
			index=standard_do+9;
		}else if(pitchSymbol.equals("6#")){
			index=standard_do+10;
		}else if(pitchSymbol.equals("7b")){
			index=standard_do+10;
		}else if(pitchSymbol.equals("7")){
			index=standard_do+11;
		}else if(pitchSymbol.equals("0")){
			index=88;
		}			
		return index;
	}
	
	
	public static List<String> readSoundFileFromTxt(){
		String filename ="E:/pitchName.txt";
		soundFileNames=FileUtil.readLines(filename);
		return soundFileNames;
	}
	
	public static void splitSoundNames(){
		String pitchNames = "a0,a0m,b0,"
				+ "c1,c1m,d1,d1m,e1,f1,f1m,g1,g1m,a1,a1m,b1,"
				+ "c2,c2m,d2,d2m,e2,f2,f2m,g2,g2m,a2,a2m,b2,"
				+ "c3,c3m,d3,d3m,e3,f3,f3m,g3,g3m,a3,a3m,b3,"
				+ "c4,c4m,d4,d4m,e4,f4,f4m,g4,g4m,a4,a4m,b4,"
				+ "c5,c5m,d5,d5m,e5,f5,f5m,g5,g5m,a5,a5m,b5,"
				+ "c6,c6m,d6,d6m,e6,f6,f6m,g6,g6m,a6,a6m,b6,"
				+ "c7,c7m,d7,d7m,e7,f7,f7m,g7,g7m,a7,a7m,b7," 
				+ "c8,c0";
		
		soundNames=pitchNames.split(",");	
	}
}
